#include "myscanaxis.h"
#include <QHBoxLayout>
#include <QPainter>

MyScanAxis::MyScanAxis(QWidget *parent) :
    QWidget(parent)
{
    pScaleWidget=new QwtScaleWidget(QwtScaleDraw::TopScale,this);
    QList<double> ticks[QwtScaleDiv::NTickTypes];
    for( int i = 0; i <=30; i++)
    {
        ticks[QwtScaleDiv::MajorTick] << i*20 ;
    }
    for( int i = 0; i <=150; i++)
    {
        ticks[QwtScaleDiv::MinorTick] << i*2;
    }
    QwtScaleDiv scaleDiv(0,300,ticks);

    pScaleWidget->setScaleDiv(scaleDiv);
    qDebug()<<"pScaleWidget->margin()"<<pScaleWidget->margin()<<pScaleWidget->spacing();
    pScaleWidget->setMargin(0);
    pScaleWidget->setSpacing(0);
    qDebug()<<"pScaleWidget->margin()1"<<pScaleWidget->margin()<<pScaleWidget->spacing();
    pWidget=new QWidget(this);
    pWidget->setMinimumHeight(21);
    //pWidget->setStyleSheet("background:blue;");
    QVBoxLayout *pVBoxLayout = new QVBoxLayout( this );
    pVBoxLayout->addWidget(pScaleWidget);
    pVBoxLayout->addWidget(pWidget);
    //pVBoxLayout->setSpacing(0);
    //pVBoxLayout->setMargin(0);
    this->setLayout(pVBoxLayout);
}

void MyScanAxis::SetScan(QList<ScanPara> scanParas,QList<QPair<double,double> > forbidRangs)
{
    this->scanParas=scanParas;
    this->forbidRangs=forbidRangs;
    this->update();
}

void MyScanAxis::paintEvent( QPaintEvent * ev )
{
    Q_UNUSED(ev)
    int x,y,width,height;
    x=pWidget->pos().x();//(9,42)
    y=pWidget->pos().y();
    width=pWidget->width();
    height=pWidget->height();
    double rate=width*1.0/300;
    QPainter painter(this);

    painter.fillRect(9,42,width,height,Qt::gray);
    for(int k=0;k<scanParas.count();k++)
    {
        double startX=scanParas.at(k).startMassNum*rate;
        double endX=scanParas.at(k).endMassNum*rate;
        double dx=endX-startX;
        if(dx<2) dx=2;
        painter.fillRect(startX,42,dx,height,Qt::blue);
    }

    for(int k=0;k<forbidRangs.count();k++)
    {
        double startX=forbidRangs.at(k).first*rate;
        double endX=forbidRangs.at(k).second*rate;
        double dx=endX-startX;
        if(dx<2) dx=2;
        painter.fillRect(startX,42,dx,height,Qt::red);
    }
}
